/*
 * locks.h
 *
 *  Created on: Jun 15, 2011
 *      Author: peckb1
 */

#ifndef ITEM_H_
#define ITEM_H_

#include "bool.h"
#include <userSharedDefinitions.h>
#include <userItem.h>

char* buf;
UserItem currentItem;
int i;
int currentAisle = 0;

typedef struct Locks {
    UserItem itemsInStore[MAX_ITEMS];
    int totalStoreSales;
    int registerSales[MAX_CLERKS];

    int _shelfLock[MAX_ITEMS]; /* Lock */
    int _itemWaitCV[MAX_ITEMS]; /* Condition */
    int _itemStockOnShelf[MAX_ITEMS];
    int _customerWaitingForReStockLock; /* Lock */
    int _stockBoyReStockingItem[MAX_ITEMS];
    int _customersWaitingForRestock[MAX_ITEMS];

    int _stockRoomStatusLock; /* Lock */
    int _stockRoomLock; /* Lock */
    int _stockBoyIdInStockRoom;
    int _stockBoysWaitingForStockRoomCV;
    int _stockRoomCV; /* Condition */
    int _stockBoyOrderLock; /* Lock */

    int _stockBoysStatusLock; /* Lock */
    int _stockBoysStatus[MAX_STOCKBOYS]; /* which Aisle we're in */

    int _stockBoyBreakLock; /* Lock */
    int _stockBoysOnBreakCV; /* Condition */
    int _stockBoyOnBreakMV; /* initial values set to zero */

    int _clerksOnBreakLock; /* Lock */
    int _clerksOnBreakCV; /* Condition */
    int _clerksOnBreakMV; /* initial values set to zero */

    int _signalManagerClerkHelp; /* Lock */
    int _storeNeedsMoreClerks[MAX_CLERKS]; /* false if lines are ok, true if a clerk saw a line > 4 */

    int _customerNeedsClerkAssitanceLock; /* Lock */
    int _customerNeedsClerkAssitanceCV; /* Condition */
    int _customerShouldFindTheClerkCV; /* Condition */
    int _customersNeedingAsistanceMV; /* initial values set to zero */
    int _clerkIDSignalingToCustomer;

    int _registerNeedsMoney; /* Lock */
    int _registerNeedsMoneyCV[MAX_CLERKS]; /* Condition */
    int _moneyNeededForTill[MAX_CLERKS];
    int _moneyGivenToTill[MAX_CLERKS];
    int _registersThatNeedMoney;

    int _customersWaitingInAisleToAskQuestionLock[NUM_CUSTOMERS_WAIT_LINES_FOR_STOCKBOYS]; /* Lock */
    int _stockBoyIdInAisleToAnswerQuestionMV[NUM_CUSTOMERS_WAIT_LINES_FOR_STOCKBOYS];
    int _customersWaitingInAisleToAskQuestionMV[NUM_CUSTOMERS_WAIT_LINES_FOR_STOCKBOYS];
    int _customersWaitingInAisleToAskQuestionCV[NUM_CUSTOMERS_WAIT_LINES_FOR_STOCKBOYS]; /* Condition */

    int _customerItemQuestionLock[MAX_STOCKBOYS]; /* Lock */
    int _customerItemQuestionMV[MAX_STOCKBOYS];
    int _customerID_AskingQuestionMV[MAX_STOCKBOYS];
    int _customerItemQuestionCV[MAX_STOCKBOYS]; /* Condition */

    int _stockBoyAisleAnswerLock[MAX_STOCKBOYS]; /* Lock */
    int _stockBoyAisleAnswerMV[MAX_STOCKBOYS];
    int _stockBoyAisleAnswerCV[MAX_STOCKBOYS]; /* Condition */

    int _clerkLinelock; /* Lock */
    int _clerkLineCV[MAX_CLERKS]; /* CV for clerks */
    int _clerkLineLength[MAX_CLERKS]; /* Line length for each clerk */
    int _clerkStatus[MAX_CLERKS]; /* MV for clerks - 0 is at register, 1 is on break */

    int _clerkLock[MAX_CLERKS]; /* Lock */
    int _clerkCV[MAX_CLERKS]; /* Condition */
    int _customerLock[MAX_CLERKS]; /* Lock */
    int _customerCV[MAX_CLERKS]; /* Condition */
    int _ItemPassingClerkLock[MAX_CLERKS]; /* Lock */
    int _ItemPassingClerkCV[MAX_CLERKS]; /* Condition */
    int _nextItem[MAX_CLERKS]; /* set to -1 for no more items */

    int _customerWaitForTotalLock[MAX_CLERKS]; /* Lock */
    int _customerWaitForTotalCV[MAX_CLERKS]; /* Condition */
    int _clerkWaitForMoneyLock[MAX_CLERKS]; /* Lock */
    int _clerkWaitForMoneyCV[MAX_CLERKS]; /* Condition */
    int _customerWaitForReceiptLock[MAX_CLERKS]; /* Lock */
    int _customerWaitForReceiptCV[MAX_CLERKS]; /* Condition */
    int _clerkWaitForCustomerLeavingLock[MAX_CLERKS]; /* Lock */
    int _clerkWaitForCustomerLeavingCV[MAX_CLERKS]; /* Condition */
    int _orderTotal[MAX_CLERKS];
    int _ammountPaid[MAX_CLERKS];
    int _receipt[MAX_CLERKS]; /* boolean */
    int _customerBeingHelped[MAX_CLERKS]; /* used for register to know what customer is being used */

    int _registerLock[MAX_CLERKS]; /* Lock */
    int _registerCV[MAX_CLERKS]; /* Condition */
    int _toRegisterInteractionLock[MAX_CLERKS]; /* Lock */
    int _toRegisterInteractionCV[MAX_CLERKS]; /* Condition */
    int _fromRegisterInteractionLock[MAX_CLERKS]; /* Lock */
    int _fromRegisterInteractionCV[MAX_CLERKS]; /* Condition */
    UserItem _itemToScan[MAX_CLERKS];
    UserItem _itemToRefund[MAX_CLERKS];
    int _moneyToTransfer[MAX_CLERKS];

    int _requestingTotal[MAX_CLERKS]; /* bool */
    int _managerEmtpyingTill[MAX_CLERKS]; /* -1 is free, 1 is with clerk, 2 is with managment */
    int _peopleWaitingForCashRegister[MAX_CLERKS];
    int _peopleWaitingForCashRegiterLock[MAX_CLERKS]; /* Lock */
    int _peopleWaitingForCashRegiterCV[MAX_CLERKS]; /* Condition */
    int _clerkWantsToInteract[MAX_CLERKS]; /*New*/
    int _managerWantsToInteract[MAX_CLERKS]; /*New*/

    int _customerActionLock; /* Lock */
    int _customerWantsToScanItems[MAX_CLERKS]; /* true if wanting to scan the item, false if return */

    int _occupancyLock; /* Lock */
    int _storeDoorCV; /* Condition */
    int _customersInStore;

    int _testLocksLock;
    int _testLocksMV;

} Locks;

Locks theLocksInHomeDepotEntry;

void InitializeLocks() {
	/* Populate things based on MAXIMUM_ITEMS */
	for(i = 0; i < MAX_ITEMS; i++) {
		int currentRandomPrice;
		if((i+1) % MAX_ITEMS_PER_AISLE == 0) {
			currentAisle ++;
		}
		theLocksInHomeDepotEntry.itemsInStore[i]._itemId = i;
		theLocksInHomeDepotEntry.itemsInStore[i]._aisle = currentAisle;
		currentRandomPrice = Rand(100);
		theLocksInHomeDepotEntry.itemsInStore[i]._price = currentRandomPrice;
		theLocksInHomeDepotEntry.itemsInStore[i]._quantity = 1;

		theLocksInHomeDepotEntry._shelfLock[i] = CreateLock("Shelf Lock"); /* Lock */
		theLocksInHomeDepotEntry._itemWaitCV[i] = CreateCV("Item Wait CV"); /* Condition */
		theLocksInHomeDepotEntry._itemStockOnShelf[i] = 0;
		theLocksInHomeDepotEntry._stockBoyReStockingItem[i] = NOT_FOUND;
		theLocksInHomeDepotEntry._customersWaitingForRestock[i] = 0;
	}

	/* Populate things that are not in a loop */
	theLocksInHomeDepotEntry._customerActionLock = CreateLock("Customer Action Lock"); /* Lock */

	theLocksInHomeDepotEntry._occupancyLock = CreateLock("Occupancy Lock"); /* Lock */
	theLocksInHomeDepotEntry._storeDoorCV = CreateCV("Store Door CV"); /* Condition */
	theLocksInHomeDepotEntry._customersInStore = 0;

	theLocksInHomeDepotEntry._clerkLinelock = CreateLock("Clerk Line Lock"); /* Lock */

	theLocksInHomeDepotEntry._registerNeedsMoney = CreateLock("Register Needs Money Lock"); /* Lock */
	theLocksInHomeDepotEntry._registersThatNeedMoney = 0;;

	theLocksInHomeDepotEntry._customerNeedsClerkAssitanceLock = CreateLock("Customer Needs Clerk Assistance Lock"); /* Lock */
	theLocksInHomeDepotEntry._customerNeedsClerkAssitanceCV = CreateCV("Customer Needs Clerk Assistance CV"); /* Condition */
	theLocksInHomeDepotEntry._customerShouldFindTheClerkCV = CreateCV("Customer Should Find The Clerk CV"); /* Condition */
	theLocksInHomeDepotEntry._customersNeedingAsistanceMV = 0; /* initial values set to zero */
	theLocksInHomeDepotEntry._clerkIDSignalingToCustomer = 0;

	theLocksInHomeDepotEntry._signalManagerClerkHelp = CreateLock("Signal Manager Clerk Help"); /* Lock */

	theLocksInHomeDepotEntry._clerksOnBreakLock = CreateLock("Clerks On Break Lock"); /* Lock */
	theLocksInHomeDepotEntry._clerksOnBreakCV = CreateCV("Clerks On Break CV"); /* Condition */
	theLocksInHomeDepotEntry._clerksOnBreakMV = 0; /* initial values set to zero */

	theLocksInHomeDepotEntry._stockBoyBreakLock = CreateLock("Stock Boy Break Lock"); /* Lock */
	theLocksInHomeDepotEntry._stockBoysOnBreakCV = CreateCV("Stock Boys On Break CV"); /* Condition */
	theLocksInHomeDepotEntry._stockBoyOnBreakMV = 0; /* initial values set to zero */

	theLocksInHomeDepotEntry._stockBoysStatusLock = CreateLock("Stock Boy Status Lock"); /* Lock */

	theLocksInHomeDepotEntry._stockRoomStatusLock = CreateLock("Stock Room Status Lock"); /* Lock */
	theLocksInHomeDepotEntry._stockRoomLock = CreateLock("Stock Room Lock"); /* Lock */
	theLocksInHomeDepotEntry._stockBoyIdInStockRoom = NOT_FOUND;
	theLocksInHomeDepotEntry._stockBoysWaitingForStockRoomCV = 0;
	theLocksInHomeDepotEntry._stockRoomCV = CreateCV("Stock Room CV"); /* Condition */
	theLocksInHomeDepotEntry._stockBoyOrderLock = CreateLock("Stock Boy Order Lock"); /* Lock */

	theLocksInHomeDepotEntry._customerWaitingForReStockLock = CreateLock("Customer Waiting for Restock Lock"); /* Lock */

	theLocksInHomeDepotEntry.totalStoreSales = 0;


	theLocksInHomeDepotEntry._testLocksLock = CreateLock("Test Lock Lock");
	theLocksInHomeDepotEntry._testLocksMV = 0;

	/* Populate things based on MAX_CLERKS */
	for(i = 0; i < MAX_CLERKS; i++) {
		theLocksInHomeDepotEntry.registerSales[i] = 0;

		theLocksInHomeDepotEntry._storeNeedsMoreClerks[i] = FALSE; /* false if lines are ok, true if a clerk saw a line > 4 */

		theLocksInHomeDepotEntry._registerNeedsMoneyCV[i] = CreateCV("Register Needs Money CV"); /* Condition */
		theLocksInHomeDepotEntry._moneyNeededForTill[i] = 0;
		theLocksInHomeDepotEntry._moneyGivenToTill[i] = 0;

		theLocksInHomeDepotEntry._clerkLineCV[i] = CreateCV("Clerk Line CV"); /* CV for clerks */
		theLocksInHomeDepotEntry._clerkLineLength[i] = 0; /* Line length for each clerk */
		theLocksInHomeDepotEntry._clerkStatus[i] = CLERK_BUSY; /* MV for clerks - 0 is at register, 1 is on break */

		theLocksInHomeDepotEntry._clerkLock[i] = CreateLock("Clerk Lock"); /* Lock */
		theLocksInHomeDepotEntry._clerkCV[i] = CreateCV("Clerk CV"); /* Condition */
		theLocksInHomeDepotEntry._customerLock[i] = CreateLock("Customer Lock"); /* Lock */
		theLocksInHomeDepotEntry._customerCV[i] = CreateCV("Customer CV"); /* Condition */
		theLocksInHomeDepotEntry._ItemPassingClerkLock[i] = CreateLock("Item Passing Clerk Lock"); /* Lock */
		theLocksInHomeDepotEntry._ItemPassingClerkCV[i] = CreateCV("Item Passing Clerk CV"); /* Condition */
		theLocksInHomeDepotEntry._nextItem[i] = 0; /* set to -1 for no more items */

		theLocksInHomeDepotEntry._customerWaitForTotalLock[i] = CreateLock("Customer Wait For Total Lock"); /* Lock */
		theLocksInHomeDepotEntry._customerWaitForTotalCV[i] = CreateCV("Customer Wait For Total CV"); /* Condition */
		theLocksInHomeDepotEntry._clerkWaitForMoneyLock[i] = CreateLock("Clerk Wait For Money Lock"); /* Lock */
		theLocksInHomeDepotEntry._clerkWaitForMoneyCV[i] = CreateCV("Clerk Wait For Money CV"); /* Condition */
		theLocksInHomeDepotEntry._customerWaitForReceiptLock[i] = CreateLock("Customer Wait For Receipt Lock"); /* Lock */
		theLocksInHomeDepotEntry._customerWaitForReceiptCV[i] = CreateCV("Customer Wait For Receipt CV"); /* Condition */
		theLocksInHomeDepotEntry._clerkWaitForCustomerLeavingLock[i] = CreateLock("Clerk Wait Got Customer Leaving Lock"); /* Lock */
		theLocksInHomeDepotEntry._clerkWaitForCustomerLeavingCV[i] = CreateCV("Clerk Wait For Customer Leaving CV"); /* Condition */
		theLocksInHomeDepotEntry._orderTotal[i] = 0;
		theLocksInHomeDepotEntry._ammountPaid[i] = 0;
		theLocksInHomeDepotEntry._receipt[i] = FALSE; /* boolean */
		theLocksInHomeDepotEntry._customerBeingHelped[i] = 0; /* used for register to know what customer is being used */

		theLocksInHomeDepotEntry._registerLock[i] = CreateLock("Register Lock"); /* Lock */
		theLocksInHomeDepotEntry._registerCV[i] = CreateCV("Create CV"); /* Condition */
		theLocksInHomeDepotEntry._toRegisterInteractionLock[i] = CreateLock("To Register Interaction Lock"); /* Lock */
		theLocksInHomeDepotEntry._toRegisterInteractionCV[i] = CreateCV("To Register Interaction CV"); /* Condition */
		theLocksInHomeDepotEntry._fromRegisterInteractionLock[i] = CreateLock("From Register Interaction Lock"); /* Lock */
		theLocksInHomeDepotEntry._fromRegisterInteractionCV[i] = CreateCV("From Register Interaction CV"); /* Condition */
		theLocksInHomeDepotEntry._itemToScan[i]._itemId = -1;
		theLocksInHomeDepotEntry._itemToScan[i]._aisle = -1;
		theLocksInHomeDepotEntry._itemToScan[i]._price = 0.0;
		theLocksInHomeDepotEntry._itemToScan[i]._quantity = -1;
		theLocksInHomeDepotEntry._itemToRefund[i]._itemId = -1;
		theLocksInHomeDepotEntry._itemToRefund[i]._aisle = -1;
		theLocksInHomeDepotEntry._itemToRefund[i]._price = 0.0;
		theLocksInHomeDepotEntry._itemToRefund[i]._quantity = -1;
		theLocksInHomeDepotEntry._moneyToTransfer[i] = 0;

		theLocksInHomeDepotEntry._requestingTotal[i] = FALSE; /* bool */
		theLocksInHomeDepotEntry._managerEmtpyingTill[i] = -2; /* -1 is free, 1 is with clerk, 2 is with managment */
		theLocksInHomeDepotEntry._peopleWaitingForCashRegister[i] = 0;
		theLocksInHomeDepotEntry._peopleWaitingForCashRegiterLock[i] = CreateLock("People Waiting For Cash Register Lock"); /* Lock */
		theLocksInHomeDepotEntry._peopleWaitingForCashRegiterCV[i] = CreateCV("People Waiting For Cash Register CV"); /* Condition */
		theLocksInHomeDepotEntry._clerkWantsToInteract[i]=-1;
		theLocksInHomeDepotEntry._managerWantsToInteract[i]=-1;
		theLocksInHomeDepotEntry._customerWantsToScanItems[i] = TRUE; /* true if wanting to scan the item, false if return */

	}

	/* Populate things based on MAX_STOCKBOYS */
	for(i = 0; i < MAX_STOCKBOYS; i++) {
		theLocksInHomeDepotEntry._stockBoysStatus[i]; /* which Aisle we're in */

		theLocksInHomeDepotEntry._customerItemQuestionLock[i] = CreateLock("Customer Item Question Lock"); /* Lock */
		theLocksInHomeDepotEntry._customerItemQuestionMV[i] = NOT_FOUND;
		theLocksInHomeDepotEntry._customerID_AskingQuestionMV[i] = NOT_FOUND;
		theLocksInHomeDepotEntry._customerItemQuestionCV[i] = CreateCV("Customer Item Question CV"); /* Condition */

		theLocksInHomeDepotEntry._stockBoyAisleAnswerLock[i] = CreateLock("Stock Boy Aisle Answer Lock"); /* Lock */
		theLocksInHomeDepotEntry._stockBoyAisleAnswerMV[i] = NOT_FOUND;
		theLocksInHomeDepotEntry._stockBoyAisleAnswerCV[i] = CreateCV("Stock Boy Aisle Answer CV"); /* Condition */
	}

	/* Populate things based on NUM_CUSTOMERS_WAIT_LINES_FOR_STOCKBOYS */
	for(i = 0; i < NUM_CUSTOMERS_WAIT_LINES_FOR_STOCKBOYS; i++) {
		theLocksInHomeDepotEntry._customersWaitingInAisleToAskQuestionLock[i] = CreateLock("Customers Waiting In Aisle To Ask Question Lock"); /* Lock */
		theLocksInHomeDepotEntry._stockBoyIdInAisleToAnswerQuestionMV[i] = NOT_FOUND;
		theLocksInHomeDepotEntry._customersWaitingInAisleToAskQuestionMV[i] = 0;
		theLocksInHomeDepotEntry._customersWaitingInAisleToAskQuestionCV[i] = CreateCV("Customers Waiting In Aisle To Ask Question CV"); /* Condition */
	}
}

#endif
